package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.annotation.UserParam;
import cn.wolfcode.luowowo.cache.service.ITravelStatisVOService;
import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Travel;
import cn.wolfcode.luowowo.domain.TravelComment;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.query.TravelCommentQuery;
import cn.wolfcode.luowowo.query.TravelQuery;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.ITravelCommentService;
import cn.wolfcode.luowowo.service.ITravelService;
import cn.wolfcode.luowowo.util.JsonResult;
import cn.wolfcode.luowowo.util.UMEditorUploader;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/travels")
public class TravelController {

    @Autowired
    private ITravelService travelService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private ITravelCommentService travelCommentService;

    @Autowired
    private ITravelStatisVOService travelStatisVOService;

    //展示游记
    @GetMapping("/detail")
    public Object  detail(String id){
        Travel travel = travelService.get(id);
        travelStatisVOService.increaseViewnum(id,1);
        return JsonResult.success(travel);
    }

    //分页查询
    @GetMapping("/query")
    public Object query(TravelQuery qo){
        Page<Travel> list = travelService.query(qo);
        return JsonResult.success(list);
    }

    //上传图片
    @RequestMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }

    @GetMapping("/input")
    public Object input(String id){
        Map<String, Object> map = new HashMap<>();
        List<Destination> dests = destinationService.list();
        map.put("dests",dests);
        if(StringUtils.hasLength(id)){
            Travel travel = travelService.get(id);
            map.put("tv",travel);
        }
        return JsonResult.success(map);
    }

    @RequireLogin
    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(Travel travel,HttpServletRequest request){
        if(StringUtils.hasLength(travel.getId())){
            travelService.update(travel,request);
        }else{
            travelService.save(travel,request);
        }
        return JsonResult.success();
    }

    //展示评论  不分页
    @GetMapping("/comments")
    public Object comments(TravelCommentQuery qo){
        List<TravelComment> comments = travelCommentService.queryComments(qo);
        return JsonResult.success(comments);
    }

    //添加评论操作
    @RequireLogin
    @PostMapping("/commentAdd")
    public Object commentAdd(TravelComment  travelComment, @UserParam UserInfo userInfo){
        BeanUtils.copyProperties(userInfo,travelComment);
        travelComment.setUserId(userInfo.getId());
        travelCommentService.save(travelComment);
        travelStatisVOService.increaseReplynum(travelComment.getTravelId(),1);
        return JsonResult.success();
    }

    //获取统计数据
    @GetMapping("/statisVo")
    public Object statisVo(String travelId){
        return JsonResult.success(travelStatisVOService.getTravelStaticVO(travelId));
    }

    //收藏操作
    @RequireLogin
    @PostMapping("/favor")
    public Object favor(String tid,@UserParam UserInfo user){
        boolean ret=travelStatisVOService.increaseFavornum(tid,user.getId());
        return JsonResult.success(ret);
    }

    //点赞操作
    @RequireLogin
    @PostMapping("/strategyThumbup")
    public Object thumbup(String tid,@UserParam UserInfo user){
        boolean ret=travelStatisVOService.increaseThunbupnum(tid,user.getId());
        return JsonResult.success(ret);
    }
}
